<?php
namespace Admin\Controller;
/* 
 * 财务报表管理
 *  
 *  */
class RecordController extends ComController{
	/* 月份报表列表页 */
	public function index(){
		//从前台接收月份
		$dateS = I('get.dateS')?I('get.dateS'):date('Y-m-d');
		$dateS = strtotime($dateS);
		$dateE = I('get.dateE')?I('get.dateE'):date('Y-m-d',strtotime('+1 month'));
		$dateE = strtotime($dateE);
		//类别 1正常  2打印
		$type =(int)I('get.type')?I('get.type'):1;
		//支付类型
		$pay_type = (int) I('get.pay_type')?I('get.pay_type'):0;
		//当前驾校id
		$sc_id = I('get.id')?I('get.id'):$_SESSION['uid'];
		$sc_name = I('get.name')?I('get.name'):$_SESSION['name'];
		if($sc_id == ''){
			$this->error('您尚未登录，请登录',U('Login/index'));
		}
		//查询预约学车订单
		$meetingPrice = $this->meetingPrice($dateS,$dateE,$sc_id,$pay_type);
		// 查询本月报名订单
		$signPrice = $this->signPrice($dateS,$dateE,$sc_id,$pay_type);
		$this->assign('sc_id',$sc_id);
		$this->assign('name',$sc_name);
		$this->assign('meetingPrice',$meetingPrice);
		$this->assign('signPrice',$signPrice);
		$this->assign('dateS',$dateS);
		$this->assign('dateE',$dateE);
		if($type ==2){
			$this->display('list_pri');
		}else{
		$this->display();
		}
	}
	//预约学车月份汇总
	public function meetingPrice($dateS,$dateE,$sc_id,$pay_type){
		$where['add_time']=array('between',array($dateS,$dateE));
		$where['school_id']=$sc_id;
		$where['ispay']=1;
		if($pay_type){
			$where['pay_type']=$pay_type;
		}
		$car_record = M('meeting_car_record');
		$car_info = $car_record
		->field('price')
		->where($where)
		->select();
		$car_price = '';
		if(!empty($car_info)){
			foreach ($car_info as $k=>$v){
				$car_price +=$v['price'];
			}
		}else{
			//当月未查询到订单
		}
		return $car_price;
	}
	
	//报名（报班）月份汇总
	public function signPrice($dateS,$dateE,$sc_id,$pay_type){
		$where['add_time']=array('between',array($dateS,$dateE));
// 		$where['DATE_FORMAT(FROM_UNIXTIME(add_time),\'%Y-%m\')']= $date;
		$where['d_id']=$sc_id;
		$where['ispay']=1;
		if($pay_type){
			$where['pay_way']=$pay_type;
		}
		$sign_record = M('sign_record');
		$sign_info = $sign_record
					->field('money')
					->where($where)
					->select();
		$sign_price ='';
		if(!empty($sign_info)){
			foreach ($sign_info as $k=>$v){
				$sign_price+=$v['money'];
			}
		}
		return $sign_price;
	}
	
	//月报表详情页
	public function details(){
		$dateS = I('get.dateS');
		$dateE = I('get.dateE');
		//支付类型  1线上支付   2线下支付
		$pay_type = (int)I('get.pay_type')?I('get.pay_type'):0;
		//type类型  1预约学车   2 报名学车
		$type = I('get.type','','intval');
		$sc_name = I('get.name')?I('get.name'):$_SESSION['name'];
		$pagesize=18;
		if(empty($dateS) or empty($dateE) or empty($type)){
			$this->error('参数错误，请重新选择');
		}
		$sc_id = I('get.id')?I('get.id'):$_SESSION['uid'];
		if($sc_id == ''){
			//未登陆
			dump('未登陆');die;
		}
		$this->assign('name',$sc_name);
		//type类型  1预约学车   2 报名学车
		if($type ==1){
			$info = $this->meetingDetails($dateS,$dateE, $sc_id,$pagesize,$pay_type);
			$this->assign('dateS',$dateS);
			$this->assign('dateE',$dateE);
			$this->assign('data', $info['data']);
			$this->assign('page', $info['page']);
			$this->assign('totalP',$info['totalPrice']);
			$this->display('Record/meetingDetails');
		}else if($type ==2){
			$info = $this->signDetails($dateS, $dateE, $sc_id, $pagesize,$pay_type);
			$this->assign('dateS',$dateS);
			$this->assign('dateE',$dateE);
			$this->assign('list', $info['list']);
			$this->assign('page', $info['page']);
			$this->assign('totalP',$info['totalP']);
			$this->display('Record/signDetails');
		}else{
			$this->error('未定义的参数，请重新选择');
		}
		
	}

	//预约学车详情页
	public function  meetingDetails($dateS,$dateE,$sc_id,$pagesize,$pay_type){
		$p= intval($_GET['p'])?$_GET['p']:1;
		$MeetingCarRecord = M('MeetingCarRecord');
		$offset = $pagesize * ($p - 1);//计算记录偏移量
		$prefix = C('DB_PREFIX');
		
		$where['mcr.school_id'] =$sc_id;
		$where['add_time']=array('between',array($dateS,$dateE));
		$where['ispay']=1;
		if($pay_type){
			$where['pay_type'] = $pay_type;
		}
			
		$count= $MeetingCarRecord->join('as mcr left join  driving_appointment_time  as at    on mcr.time_id=at.id')
		->field('mcr.id')
		->where($where)
		->order('mcr.id asc')
		->count();
		
		$data= $MeetingCarRecord->join('as mcr left join  driving_appointment_time  as at    on mcr.time_id=at.id')
		->field('mcr.id, mcr.user_name,mcr.user_phone, mcr.coach_name,mcr.coach_head, mcr.day,  mcr.category, mcr.course, mcr.status,mcr.add_time,mcr.coach_phone,mcr.site,mcr.car_num,mcr.type,mcr.ispay,mcr.price,mcr.pay_type,at.time,at.time as study_hours')
		->where($where)
		->order('mcr.id asc' )
		->limit($offset . ',' . $pagesize)
		->select();
		$totalPrice = '';
		foreach($data as $k => $v){
			$arr = explode('-',$v['study_hours']);
			$time = round((strtotime($arr[1]) - strtotime($arr[0])) / 3600 );
			$data[$k]['study_hours'] = $time;
			$totalPrice +=$v['price'];
			
		}
	
		$page = new \Think\Page($count, $pagesize);
		$page = $page->show();
		$info = array('data'=>$data,'page'=>$page,'totalPrice'=>$totalPrice);
		return $info;
	}
	//在线报名详情页
	public function signDetails($dateS,$dateE,$sc_id,$pagesize,$pay_type){
		$p = isset($_GET['p']) ? intval($_GET['p']) : '1';
		$sign = M('sign_record');
		$prefix = C('DB_PREFIX');
		$where["{$prefix}sign_record.d_id"] =$sc_id;
		$where["{$prefix}sign_record.add_time"]=array('between',array($dateS,$dateE));
		$where["{$prefix}sign_record.is_paid"]=1;
		if($pay_type){
			$where['pay_way']=$pay_type;
		}
		
		$offset = $pagesize * ($p - 1);//计算记录偏移量
		$count = $sign->where($where)->count();
		
		$list = $sign
		->where($where)
		->field("{$prefix}sign_record.*,{$prefix}member.name as school_name,{$prefix}class_level.type,{$prefix}class_level.name as class_name")
		->join("{$prefix}member ON {$prefix}member.uid = {$prefix}sign_record.d_id")
		->join("{$prefix}class_level ON {$prefix}class_level.id = {$prefix}sign_record.c_id")
		->order('id desc')
		->limit($offset . ',' . $pagesize)
		->select();
		$totalP ='';
		foreach ($list as $k=>$v){
			$totalP +=$v['money'];
		}
		$page = new \Think\Page($count, $pagesize);
		$info = array('list'=>$list,'page'=>$page,'totalP'=>$totalP);
		return $info;
	}
	
	public function selectSchool(){
		$m = M('member');
		$info = $m->field('uid,name')->select();
		echo  returnAjax($info);
	}
	
	
	
	
	
}



